//
//  HOHybridTypeViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 6/15/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOHybridTypeViewController.h"
#import "HOConstants.h"
#import "HOStaticDataMgr.h"
#import "HOFaultCategoryEntity.h"
#import "HOUtils.h"
#import "HOGlobalData.h"

static BOOL isRequestedDept;
@interface HOHybridTypeViewController ()<UISearchBarDelegate>
{
	UITableView *secondTableView;
	UITableView *thirdTableView;
	HO_SEARCH_STATUS searchStatus;
}
@property(nonatomic, retain)NSMutableDictionary *searchedTypeDic;
@property(nonatomic, retain)HOFaultCategoryEntity *currentEntityFromSearch;
@property(nonatomic, retain)HOFaultCategoryEntity *subEntityFromSearch;

@property(nonatomic, retain)HOFaultCategoryEntity *selectTypeEntyity;
@property(nonatomic, retain)HOFaultCategoryEntity *selectSubTypeEntity;
@property(nonatomic, retain)NSString *searchKey;
@end

@implementation HOHybridTypeViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view.
	[self initDataSource];
	[self initTableView];
	[self initSearchBar];
	[self initSubviews];
}

- (void)initDataSource
{
	self.searchedTypeDic = [[[NSMutableDictionary alloc] init] autorelease];
}




- (void)initSubviews
{
	[self.view setBackgroundColor:[UIColor whiteColor]];
	baseTableview.frame = CGRectMake(0,
									 baseTableview.frame.origin.y,
									 self.view.frame.size.width / 3,
									 baseTableview.frame.size.height);
	
	secondTableView = [[UITableView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 3 - 22,
																   baseTableview.frame.origin.y,
																   self.view.frame.size.width / 3 + 22,
																   baseTableview.frame.size.height - 50)
												  style:UITableViewStyleGrouped];
	[secondTableView setBackgroundColor:MAIN_BG_COLOR];
	secondTableView.delaysContentTouches = NO;
	secondTableView.delegate = self;
	secondTableView.dataSource = self;
	secondTableView.sectionFooterHeight = 0;
	secondTableView.separatorColor = TEXT_GRAY_DARK_dbdbdb;
	[self.view addSubview:secondTableView];
	
	
	
	thirdTableView = [[UITableView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 3 - 22,
																  baseTableview.frame.origin.y,
																  self.view.frame.size.width / 3 + 22,
																  baseTableview.frame.size.height - 50)
												 style:UITableViewStyleGrouped];
	[thirdTableView setBackgroundColor:MAIN_BG_COLOR];
	thirdTableView.delaysContentTouches = NO;
	thirdTableView.delegate = self;
	thirdTableView.dataSource = self;
	thirdTableView.sectionFooterHeight = 0;
	thirdTableView.separatorColor = TEXT_GRAY_DARK_dbdbdb;
	[self.view addSubview:thirdTableView];
}



#pragma mark -
#pragma mark search UI

- (void)initSearchBar
{
	UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0,
															 0,
															 self.view.frame.size.width,
															 44)]autorelease] ;
	[view setBackgroundColor:TEXT_COLOR_E3E3E3];
	UISearchBar *searchBar = [[[UISearchBar alloc]
							   initWithFrame:CGRectMake(0,
														0,
														self.view.frame.size.width,
														44)] autorelease];
	
	[searchBar setBackgroundImage:[HOUtils createImageWithColor:TEXT_COLOR_E3E3E3 forView:searchBar]
				   forBarPosition:0
					   barMetrics:UIBarMetricsDefault];
	
	searchBar.showsCancelButton = YES;
	searchBar.delegate = self;
	searchBar.placeholder = NSLocalizedString(@"输入关键字", @"输入关键字");
	searchBar.tintColor = MAIN_COLOR_NORMAL;
	searchBar.showsCancelButton = YES;
	searchBar.keyboardType = UIKeyboardTypeDefault;
	[searchBar setBackgroundColor:[UIColor clearColor]];
	[view addSubview:searchBar];
	[self.view addSubview:view];
	[self updateSubviewFrame];
	
}


- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
	//	NSLog(@"输入搜索内容完毕");
	
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;                      // return NO to not become first responder
{
	if(searchStatus == HO_SEARCH_SEARCHING)
		return NO;                   // return NO to not become first responder
	return YES;
	
}


//取消的响应事件
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
	
	//	NSLog(@"取消搜索");
	[searchBar resignFirstResponder];
	searchStatus = HO_SEARCH_CLOSED;
	[[self getSearchedFirstTypeList] removeAllObjects];
	self.currentEntityFromSearch = nil;
	self.subEntityFromSearch = nil;
	self.searchKey = nil;
	[self selectTheDefault];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
	if(searchText.length == 0)
	{
		[searchBar endEditing:YES];
		searchStatus = HO_SEARCH_CLOSED;
		[self selectTheDefault];
	}
}

//键盘上搜索事件的响应
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
	
	//	NSLog(@"搜索");
	//	[searchBar setShowsCancelButton:NO animated:YES];
	[searchBar resignFirstResponder];
	self.searchKey = [[searchBar text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
	searchStatus = HO_SEARCH_SEARCHED;
	[[self getSearchedFirstTypeList] removeAllObjects];
	[self selectTheDepartmentDefault];
	[secondTableView reloadData];
	[thirdTableView reloadData];
	[searchBar resignFirstResponder];
	[self requestType];
}


////在搜索框中修改搜索内容时，自动触发下面的方法
//-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
//	return YES;
//}
//
//#pragma mark - -- UISearchBarDelegate impelemnt methods
//// called when keyboard search button pressed
//- (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar
//{
//	NSString *key = [[aSearchBar text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//	[self gotoSearchKey:key];
//
//}
//
//- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
//{
//	[self requestType:nil];
//}

//- (void)gotoSearchKey:(NSString *)key
//{
//	//	if(key.length < 2)
//	//	{
//	//		[self popupHintView:@"请输入两字或者两字以上关键字搜索"];
//	//		return;
//	//	}
//
//	[self requestType];
//}


- (void)requestType
{
	if(searchStatus == HO_SEARCH_CLOSED && [[self getFirstTypeList] count] > 0)
	{
		BOOL needRequest = NO;
		for(int i = 0;  i < [[self getFirstTypeList] count]; i ++)
		{
			HOFaultCategoryEntity *entity = [[self getFirstTypeList] objectAtIndex:i];
			if([entity.subTypes count] == 0)
			{
				needRequest = YES;
				break;
			}
		}
		if(!needRequest)
		{
			[self selectTheDefault];
			return;
		}
	}
	
	
	NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{kHO_FIELD_Action:kHO_ACTION_GetFaultTypeList,
																			   kHO_FIELD_DepartmentId:[NSNumber numberWithInt:self.currentDepartmentEntity.departmentId]}];
	if(self.searchKey != nil && self.searchKey.length > 0)
	{
		[dic setObject:self.searchKey forKey:kHO_FIELD_Keywords];
	}
	[self sendRequestWithParams:dic];
}

- (void)requestDepartmentWithMaxId:(int)maxId
{
	if([[self getDepartmentList] count] == 0 || !isRequestedDept)
	{
		
		[[self getDepartmentList] removeAllObjects];
		[self selectTheDepartmentDefault];
		[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_Department,
									  kHO_FIELD_MaxID:[NSNumber numberWithInt:maxId]}];
		isRequestedDept = YES;
	}
	else
	{
		[self selectTheDepartmentDefault];
	}
}

- (void)viewWillAppear:(BOOL)animated
{
	self.tabBarController.tabBar.hidden = YES;
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"选择故障分类", @"选择故障分类")];
	[self updateSubviewFrame];
	[self requestDepartmentWithMaxId:1];
}

- (void)updateSubviewFrame
{
	CGFloat width = self.view.frame.size.width /3;
	baseTableview.frame = CGRectMake(0,
									 44,
									 width,
									 [HOUtils getScreenHeight] - 44 - self.navigationController.navigationBar.frame.size.height - [HOUtils getStatusBarHeight]);
	secondTableView.frame = CGRectMake(width,
									   baseTableview.frame.origin.y,
									   width,
									   baseTableview.frame.size.height);
	thirdTableView.frame = CGRectMake(width * 2,
									   baseTableview.frame.origin.y,
									   width,
									   baseTableview.frame.size.height);
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}
#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSString *content = @"";
	
	if(tableView == baseTableview)
	{
		HODepartmentEntity *department = [[self getDepartmentList] objectAtIndex:indexPath.row];
		
		content =  department.departmentName;
	}
	else if(tableView == secondTableView)
	{
		HOFaultCategoryEntity *entity = nil;
		if(searchStatus == HO_SEARCH_SEARCHED)
		{
			entity = [[self getSearchedFirstTypeList] objectAtIndex:indexPath.row];
		}
		else
		{
			
			entity = [[self getFirstTypeList] objectAtIndex:indexPath.row];
		}
		content =  entity.faultDetail;
	}
	else if(tableView == thirdTableView)
	{
		HOFaultCategoryEntity *entity = nil;
		if(searchStatus == HO_SEARCH_SEARCHED)
		{
			entity = [self.currentEntityFromSearch.subTypes objectAtIndex:indexPath.row];
		}
		else
		{
			
			entity = [self.selectTypeEntyity.subTypes objectAtIndex:indexPath.row];
		}
		content = entity.faultDetail;
	}
	CGSize contentSize = [HOUtils findHeightForText:content
										havingWidth:(tableView.frame.size.width - 15)
											andFont:[UIFont systemFontOfSize:13]];
	CGFloat height = MAX(22, contentSize.height);
	return height + 22;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	if(tableView == baseTableview)
	{
		NSInteger count = 0;
		count = [[self getDepartmentList] count];
		return count;
	}
	else if(tableView == secondTableView)
	{
		NSInteger count = 0;
		if(searchStatus == HO_SEARCH_SEARCHED)
		{
			count = [[self getSearchedFirstTypeList] count];
		}
		else
		{
			count = [[self getFirstTypeList] count];
		}
		return count;
	}
	else if(tableView == thirdTableView)
	{
		NSInteger count = 0;
		if(searchStatus == HO_SEARCH_SEARCHED)
		{
			if(!self.currentEntityFromSearch)
				return 0;
			count = [self.currentEntityFromSearch.subTypes count];
			return count;
		}
		else
		{
			if(!self.selectTypeEntyity)
				return 0;
			count = [self.selectTypeEntyity.subTypes count];
			return count;
		}
		
	}
	return 0;
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return SECTION_PADDING / 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *AMBaseTableViewCellIdentifier = @"HOTableViewCell";
	HOTableViewCell *cell = (HOTableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
	if(cell == nil)
	{
		cell = [[[HOTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
	}
	UILabel *lbl =(UILabel *)[cell viewWithTag:112];
	if(!lbl)
	{
		lbl = [[[UILabel alloc] initWithFrame:CGRectMake(4,
														 (CELL_HEIGHT - 22) / 2,
														 tableView.frame.size.width - 15,
														 22)] autorelease];
		lbl.tag = 112;
		lbl.numberOfLines = 3;
//		lbl.layer.borderWidth = 1;
		lbl.font = [UIFont systemFontOfSize:13];
		lbl.textColor = TEXT_COLOR_181818;
		[cell.contentView addSubview:lbl];
	}
	UIImageView *imgView =(UIImageView *)[cell viewWithTag:113];
	if(!imgView)
	{
		imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(tableView.frame.size.width - 12,
																 (CELL_HEIGHT - 16) / 2,
																 9,
																 16)] autorelease];
		imgView.image = [UIImage imageNamed:@"ic_taizhuanyemian"];
		imgView.tag = 113;
		
		[cell.contentView addSubview:imgView];
	}
	cell.textLabel.font = [UIFont systemFontOfSize:14];
	cell.textLabel.textColor = TEXT_COLOR_181818;
	cell.selectionStyle = UITableViewCellSelectionStyleGray;
	//	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	if(tableView == baseTableview)
	{
		HODepartmentEntity *department = [[self getDepartmentList] objectAtIndex:indexPath.row];
		
		lbl.text =  department.departmentName;
	}
	else if(tableView == secondTableView)
	{
		HOFaultCategoryEntity *entity = nil;
		if(searchStatus == HO_SEARCH_SEARCHED)
		{
			entity = [[self getSearchedFirstTypeList] objectAtIndex:indexPath.row];
		}
		else
		{
			
			entity = [[self getFirstTypeList] objectAtIndex:indexPath.row];
		}
		lbl.text =  entity.faultDetail;
	}
	else if(tableView == thirdTableView)
	{
		HOFaultCategoryEntity *entity = nil;
		if(searchStatus == HO_SEARCH_SEARCHED)
		{
			entity = [self.currentEntityFromSearch.subTypes objectAtIndex:indexPath.row];
		}
		else
		{
			
			entity = [self.selectTypeEntyity.subTypes objectAtIndex:indexPath.row];
		}
		lbl.text = entity.faultDetail;
	}
	[lbl sizeToFit];
	CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
	lbl.frame = CGRectMake(3,
						   (cellHeight - lbl.frame.size.height) / 2,
						   tableView.frame.size.width - 15,
						   lbl.frame.size.height);
	
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	//	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	if(tableView == baseTableview)
	{
		if(indexPath.row >= [[self getDepartmentList] count])
		{
			return;
		}
		self.currentDepartmentEntity = [[self getDepartmentList] objectAtIndex:indexPath.row];
		self.currentEntityFromSearch = nil;
		self.subEntityFromSearch = nil;
		[secondTableView clearsContextBeforeDrawing];
		[thirdTableView clearsContextBeforeDrawing];
		[self requestType];
		
	}
	else if(tableView == secondTableView)
	{
		HOFaultCategoryEntity *entity = nil;
		
		if(searchStatus == HO_SEARCH_SEARCHED)
		{
			entity = [[self getSearchedFirstTypeList] objectAtIndex:indexPath.row];
			self.currentEntityFromSearch = entity;
		}
		else
		{
			entity = [[self getFirstTypeList] objectAtIndex:indexPath.row];
			self.currentEntity = entity;
			self.selectTypeEntyity = entity;
		}
		
		[thirdTableView reloadData];
	}
	else if(tableView == thirdTableView)
	{
		HOFaultCategoryEntity *entity = nil;
		if(searchStatus == HO_SEARCH_SEARCHED)
		{
			entity = [self.currentEntityFromSearch.subTypes objectAtIndex:indexPath.row];
			self.subEntityFromSearch = entity;
		}
		else
		{
			entity = [self.selectTypeEntyity.subTypes objectAtIndex:indexPath.row];
			self.selectSubTypeEntity = entity;
		}
		if(entity.faultId == 106)
		{
			HOAlertView *alert = [[HOAlertView alloc] initWithTitle: @"温馨提示"
															message:@"请先让清洁部确认是否管工处理!"
														   delegate: self
												  cancelButtonTitle:@"取消选择"
												  otherButtonTitles:@"继续选择",nil];
			alert.tag = [kHO_DIALOG_Confirm_FaultType integerValue];
			[alert show];
			[alert release];
		}
		else
		{
			[self didSelectFaultCategory];
		}
		
	}
	
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
	[super alertView:alertView clickedButtonAtIndex:buttonIndex];
	if(alertView.tag == [kHO_DIALOG_Confirm_FaultType integerValue])
	{
		if(buttonIndex == 1)
		{
			[self didSelectFaultCategory];
		}
	}
}

- (void)didSelectFaultCategory
{
	if(searchStatus == HO_SEARCH_SEARCHED)
	{
		[self.delegate didSelectFault:self.currentEntityFromSearch withSubEntity:self.subEntityFromSearch withDepartment:self.currentDepartmentEntity];
	}
	else
	{
		[self.delegate didSelectFault:self.selectTypeEntyity withSubEntity:self.selectSubTypeEntity withDepartment:self.currentDepartmentEntity];
	}
	[self goBack];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (void)dealloc
{
	self.delegate = nil;
	[secondTableView release];
	secondTableView.delegate = nil;
	secondTableView.dataSource = nil;
	thirdTableView.delegate = nil;
	thirdTableView.dataSource = nil;
	[thirdTableView release];
	self.currentEntityFromSearch = nil;
	self.subEntityFromSearch = nil;
	self.currentDepartmentEntity = nil;
	self.searchedTypeDic = nil;
	self.searchKey = nil;
	self.selectTypeEntyity = nil;
	self.selectSubTypeEntity = nil;
	[super dealloc];
}

- (NSMutableArray *)getFirstTypeList
{
	NSMutableArray *array = [[HOStaticDataMgr sharedInstance] getFirstTypeArrayWithDepartmentId:self.currentDepartmentEntity.departmentId];
	return array;
}

- (NSMutableArray *)getDepartmentList
{
	NSMutableArray *array = [HOStaticDataMgr sharedInstance].departmentList;
	return array;
}

- (NSMutableArray *)getSearchedFirstTypeList
{
	NSMutableArray *array = [self.searchedTypeDic objectForKey:[NSNumber numberWithInt:self.currentDepartmentEntity.departmentId]];
	if(array == nil)
	{
		array = [[[NSMutableArray alloc] init] autorelease];
		[self.searchedTypeDic setObject:array forKey:[NSNumber numberWithInt:self.currentDepartmentEntity.departmentId]];
		
	}
	return array;
}


#pragma mark
- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	
	NSString *keyboard = [response.baseRequest.params objectForKey:kHO_FIELD_Keywords];
	if(keyboard)
	{
		[[self getSearchedFirstTypeList] removeAllObjects];
	}
	if([response isEqualAction:kHO_ACTION_GetFaultTypeList])
	{
		        NSData *data = [NSJSONSerialization dataWithJSONObject:response.dataNode options:NSJSONWritingPrettyPrinted error:nil];
		        NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
		        NSLog(@"%@",str);
		if(![response.dataNode isKindOfClass:[NSArray class]])
		{
			if(keyboard)
			{
				self.currentEntityFromSearch = nil;
				self.subEntityFromSearch = nil;
				[secondTableView reloadData];
				[thirdTableView reloadData];
			}
			return;
		}
		[[self getSearchedFirstTypeList] removeAllObjects];
		NSArray *array = response.dataNode;
		for(int i = 0; i < [array count]; i ++)
		{
			NSDictionary *subDic = [array objectAtIndex:i];
			
			int subFaultId = [[subDic objectForKey:kHO_FIELD_FaultId] intValue];
			HOFaultCategoryEntity *entity = [[HOStaticDataMgr sharedInstance] getFaultCategoryEntityWithId:subFaultId];
			[entity parseDic:subDic];
			if(keyboard)
			{
				[[self getSearchedFirstTypeList] addObject:entity];
				self.currentEntityFromSearch = [self getSearchedFirstTypeList].firstObject;
				
			}
			else
			{
				entity.index = i;
				if(![[self getFirstTypeList] containsObject:entity])
				{
					[[self getFirstTypeList] addObject:entity];
				}
			}
			
		}
		if(keyboard)
		{
			[self selectTheDefaultFromSearch];
		}
		else
		{
			[self selectTheDefault];
		}
	}
	else if([response isEqualAction:kHO_ACTION_Department])
	{
		if(![response.dataNode isKindOfClass:[NSArray class]])
		{
			return;
		}
		NSArray *array = response.dataNode;
		[[self getDepartmentList] removeAllObjects];
		for(NSInteger i = 0; i < [array count];i ++)
		{
			NSDictionary *dic = [array objectAtIndex:i];
			int entityId = [[dic objectForKey:kHO_FIELD_DepartmentId] intValue];
			HODepartmentEntity *entity = [[HOStaticDataMgr sharedInstance] getDepartmentEntityWithId:entityId];
			[entity parseDic:dic];
			entity.index = i;
			[[self getDepartmentList] addObject:entity];
			
		}
		[self selectTheDepartmentDefault];
	}
}

- (void)selectTheDepartmentDefault
{
	if([[self getDepartmentList] count] == 0)
	{
		[baseTableview clearsContextBeforeDrawing];
		[baseTableview reloadData];
		return;
	}
	[baseTableview reloadData];
	if(self.currentDepartmentEntity == nil)
	{
		self.currentDepartmentEntity = [self getDepartmentList].firstObject;
	}
	if(self.currentDepartmentEntity.index < [[self getDepartmentList] count])
	{
		[baseTableview selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.currentDepartmentEntity.index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
		[baseTableview scrollsToTop];
	}
	
	[self requestType];
}

- (void)selectTheDefault
{
	if([[self getFirstTypeList] count] == 0)
		return;
	[secondTableView reloadData];
	self.selectTypeEntyity = self.currentEntity;
	self.selectSubTypeEntity = self.subEntity;
	if(self.selectTypeEntyity == nil || [self.selectTypeEntyity.subTypes count] == 0 || ![[self getFirstTypeList] containsObject:self.selectTypeEntyity])
		self.selectTypeEntyity = [[self getFirstTypeList] firstObject];
	if(self.selectTypeEntyity.index < [[self getFirstTypeList] count])
	{
		[secondTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectTypeEntyity.index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
		[secondTableView scrollsToTop];
	}
	NSArray *secondAreaList = self.selectTypeEntyity.subTypes;
	if([secondAreaList count] == 0)
		return;
	
	[thirdTableView reloadData];
	
	if(self.selectSubTypeEntity == nil || ![secondAreaList containsObject:self.selectSubTypeEntity])
		self.selectSubTypeEntity = [secondAreaList firstObject];
	if(self.selectSubTypeEntity.index < [secondAreaList count])
	{
		[thirdTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectSubTypeEntity.index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
		[thirdTableView scrollsToTop];
	}
	
}


- (void)selectTheDefaultFromSearch
{
	if([[self getSearchedFirstTypeList] count] == 0)
	{
		[secondTableView reloadData];
		[thirdTableView reloadData];
		return;
	}
	[secondTableView reloadData];
	if(self.currentEntityFromSearch == nil || [self.currentEntityFromSearch.subTypes count] == 0)
		self.currentEntityFromSearch = [[self getSearchedFirstTypeList] firstObject];

	[secondTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
	[secondTableView scrollsToTop];
	NSArray *secondAreaList = self.currentEntityFromSearch.subTypes;
	if([secondAreaList count] == 0)
		return;
	
	[thirdTableView reloadData];
	
	if(self.subEntityFromSearch == nil)
		self.subEntityFromSearch = [secondAreaList firstObject];
	
	[thirdTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
	[thirdTableView scrollsToTop];
	
}


@end

